#ifndef GROUND_HF_H
#define GROUND_HF_H

#include "ActorEntity.h"
#include "../../SurfaceRenderingData.h"
#include "../../WrappedContainers/LinkedList.h"

class Ground : public ActorEntity {
protected:
	typedef short HeightFieldElevationType;

	NxActor* actor;

	const NxU32 cellCountX;
	const NxU32 cellCountZ;
	const float cellsPerTexture;
	const NxU32 rx;
	const NxU32 rz;
	const NxU32 tx;
	const NxU32 tz;

	unsigned int vboVertexBuffer;
	unsigned int vboNormalBuffer;
	unsigned int vboColorBuffer;
	unsigned int vboTexCoordBuffer;
	unsigned int vboIndexBuffer;

	static SurfaceRenderingData srd;

public:
	static LinkedList<Ground*> all;
	static LinkedList<Ground*> toDelete;

protected:
	static inline NxHeightFieldSample* GetSampleFromIndex(NxU8* samples, NxU32 index, NxU32 sampleStride) {
		return (NxHeightFieldSample*)(samples + sampleStride * index);
	}

	inline float GetTextureCoordinateX(int sampleX, bool border) {
		return border ? (sampleX-1) % tx / cellsPerTexture + 1 / cellsPerTexture : sampleX % tx / cellsPerTexture;
	}

	inline float GetTextureCoordinateZ(int sampleZ, bool border) {
		return border ? (sampleZ-1) % tz / cellsPerTexture + 1 / cellsPerTexture : sampleZ % tz / cellsPerTexture;
	}

public:
	static void Load();

	Ground(NxU32 cellCountX, NxU32 cellCountZ, NxU32 planeDistance, float roughness, float cellsPerTexture, const NxVec3& scale);
	virtual ~Ground();

	virtual void Draw();

	virtual inline ACTOR_ENTITY_TYPE GetType() const { return AET_GROUND; }

};

#endif //GROUND_HF_H
